package com.baomidou.springwind.controller.supply;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.controller.NodeData;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.mapper.*;
import com.baomidou.springwind.pojo.SiteCoverBuilder;
import com.baomidou.springwind.pojo.SiteSelection;
import com.baomidou.springwind.pojo.SiteSelectionBuilder;
import com.baomidou.springwind.pojo.SiteSelectionBuilderTwo;
import com.baomidou.springwind.service.*;
import com.baomidou.springwind.utils.CityUtil;
import com.baomidou.springwind.utils.ParamMap;
import com.baomidou.springwind.utils.ZvalueUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/network/supply")
public class AlgorithmOneController extends BaseController{
    @Autowired
    private NetworkSupplyService networkSupplyService;

    @Autowired
    private NetworkSupplyDao networkSupplyDao;

    @Autowired
    private NetworkDeliveryDao networkDeliveryDao;

    @Autowired
    private NetworkDeliveryService networkDeliveryService;

    @Autowired
    private OrderDataService orderDataService;

    @Autowired
    private OrderDataDao orderDataDao;

    @Autowired
    private NetworkPlanningDao networkPlanningDao;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private NetworkCandidateService candidateService;

    @Autowired
    private NetworkTransportPropertyService propertyService;

    @Autowired
    private NetworkAlgorithmStateDao algorithmStateDao;

    private DecimalFormat df = new DecimalFormat("###,##0.00");

    /**
     * 客户分析主界面
     */
    @RequestMapping("/algorithm1")
    @Permission("1001")
    public String algorithm(Model model) {
        return pjaxPath("/lpp/network/supply/algorithmOne/main",model);
    }

    /**
     * 判断缺少什么数据，处于哪个步骤了
     * @return
     */
    @ResponseBody
    @RequestMapping("/algorithm1/getAllData")
    @Permission("1001")
    public String getAllData() {
        // 0 是优化后的  1是目前现状的
        JSONObject ret = new JSONObject();
        int i=0;
        EntityWrapper<OrderData> wrapper1 = new EntityWrapper<OrderData>();
        wrapper1.eq("batch_id", getProjectId());
        if(orderDataService.selectCount(wrapper1)==0){
            i=0;
            ret.put("state",i);
            return callbackSuccess(ret);
        }
        EntityWrapper<NetworkTransportProperty> wrapper3 = new EntityWrapper<NetworkTransportProperty>();
        wrapper3.eq("batch_id", getProjectId());
        wrapper3.where("transport is not null");
        int i3=propertyService.selectCount(wrapper3);
        if(i3==0){
            i=1;
            ret.put("state",i);
            return callbackSuccess(ret);
        }
        EntityWrapper<NetworkCandidate> wrapper2 = new EntityWrapper<NetworkCandidate>();
        wrapper2.eq("batch_id", getProjectId());
        if(candidateService.selectCount(wrapper2)==0){
            i=2;
            ret.put("state",i);
            return callbackSuccess(ret);
        }

        EntityWrapper<NetworkAlgorithmState> wrapper4 = new EntityWrapper<NetworkAlgorithmState>();
        wrapper4.eq("batch_id", getProjectId());
        if(algorithmStateDao.selectCount(wrapper4)==0){
            i=5;
            ret.put("state",i);
            return callbackSuccess(ret);
        }
        wrapper4.eq("state",0);
        if(algorithmStateDao.selectCount(wrapper4)==1){
            i=6;
            ret.put("state",i);
            return callbackSuccess(ret);
        }
        i=9;
        ret.put("state",i);
        return callbackSuccess(ret);
    }







}
